package com.coincalf.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.coincalf.dto.CoinInfoDto;
import com.coincalf.entity.Coin;
import com.coincalf.framework.enums.BaseStatus;
import com.coincalf.framework.enums.WalletType;
import com.coincalf.framework.http.Response;
import com.coincalf.service.CoinService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 币币交易币种Controller
 */
@Slf4j
@RestController
@RequestMapping("/coin")
public class CoinController {

    @Autowired
    private CoinService coinService;

    /**
     * 查询有效的钱包币
     *
     * @return
     */
    @GetMapping("/trade/wallet")
    @ApiOperation(value = "COIN-001 获取钱包币信息", notes = "获取钱包币信息", httpMethod = "GET")
    public Object walletCoin() {
        EntityWrapper<Coin> wrapper = new EntityWrapper<>();
        wrapper.eq("status", BaseStatus.EFFECTIVE.getCode())
                .eq("wallet", WalletType.QBB.getCode());
        return Response.ok(coinService.selectList(wrapper));
    }

    /**
     * 查找数字货币基础币
     */
    @GetMapping("/baseCoin")
    @ApiOperation(value = "获取数字货币基础币列表", notes = "获取数字货币基础币列表", httpMethod = "GET")
    public Object baseCoin() {
        EntityWrapper<Coin> wrapper = new EntityWrapper<>();
        return Response.ok(coinService.selectList(wrapper));
    }

    @GetMapping("/serverUrl/{coinName}")
    public String getCoinRpcServerUrl(@PathVariable("coinName") String coinName) {
        return coinService.queryServerUrl(coinName);
    }

    @GetMapping("/info/{coinName}")
    public CoinInfoDto selectIsMain(@PathVariable("coinName") String coinName) {
        return coinService.queryInfo(coinName);
    }

    @GetMapping("/mainCoins")
    public List<CoinInfoDto> selectMainCoins() {
        List<Coin> coins = coinService.queryMainCoins();
        List<CoinInfoDto> result = new ArrayList<>(coins.size());
        for (Coin coin : coins) {
            CoinInfoDto coinInfoDto = new CoinInfoDto();
            coinInfoDto.setCoinId(coin.getId());
            coinInfoDto.setCoinName(coin.getName());
            coinInfoDto.setMain(coin.getIsMain());
            result.add(coinInfoDto);
        }
        return result;
    }

    @GetMapping("/contract/{coinId}")
    public List<CoinInfoDto> selectContractAddressesByMainCoinId(@PathVariable("coinId") Long mainCoinId) {
        return coinService.selectContractAddressesByMainCoinId(mainCoinId);
    }

    @GetMapping("/mainCoinId/{coinId}")
    public Long selectMainCoinIdByCoinId(@PathVariable("coinId") Long coinId) {
        return coinService.selectMainCoinIdByCoinId(coinId);
    }

    // TODO: 2021/2/4 推荐币种 暂时写死 后续由数据库获取
//    @GetMapping("/recommend")
//    public Response recommend() {
//
//    }

}
